﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Restaurant.Model;
using Dapper;
using DapperExtensions;
using Restaurant.Model.ExModel;

namespace Restaurant.Dao
{
    public class FoodTypeDao : BaseServiceDao<FoodType>
    {
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public override Task<FoodType> SaveAsync(FoodType model)
        {
            model.FoodTypeId = Guid.NewGuid();
            return base.SaveAsync(model);
        }
        public  Task<FoodType_Food_Ex> AddModeEx(FoodType_Food_Ex model)
        {
            model.FoodTypeId = Guid.NewGuid();
            return base.SaveAsync<FoodType_Food_Ex>(model);

        }
        /// <summary>
        /// 获取分类关联菜品
        /// </summary>
        /// <param name="subId"></param>
        /// <returns></returns>
        public async Task<Tuple<IEnumerable<FoodType_Food_Ex>, int>> GetAllExAsync(Guid subId)
        {
            string sql = "SELECT a.*,b.* FROM FoodType  a left join Food b on a.FoodTypeId = b.FoodType_Id where a.subCompany_Id = @subId";

            var lookup = new Dictionary<Guid, FoodType_Food_Ex>();

            var list = await dbConnection.QueryAsync<FoodType_Food_Ex, Food, FoodType_Food_Ex>(sql, (g, r) =>
            {
                FoodType_Food_Ex tmp;
                if (!lookup.TryGetValue(g.FoodTypeId, out tmp))
                {
                    tmp = g;
                    lookup.Add(g.FoodTypeId, tmp);
                }

                tmp.ListFood.Add(r);

                return g;
            }, new { subId = subId }, splitOn: "id");

            return new Tuple<IEnumerable<FoodType_Food_Ex>, int>(lookup.Values.ToList(), lookup.Count);


        }


    }
}
